#include "transmissionpage.h"
#include "ui_transmissionpage.h"
#include<QDebug>
#include"mainwindow.h"

TransmissionPage::TransmissionPage(int id,std::shared_ptr<CloudDrive::UploadControl> contral,QWidget *parent) :
    QWidget(parent),
    ui(new Ui::TransmissionPage)
{
    ui->setupUi(this);
    windowId = id;
    qDebug()<<id;
    m_contral = contral;
    ui->progressBar->setValue(0);
    qDebug()<<windowId;
    QObject::connect(&mainWindow::getInstance(),&mainWindow::ProgressBar,this,&TransmissionPage::setProgressBar,Qt::QueuedConnection);
    QObject::connect(this,&TransmissionPage::funishSignal,&mainWindow::getInstance(),&mainWindow::startFileUploadFunish,Qt::QueuedConnection);
}

TransmissionPage::~TransmissionPage()
{
    delete ui;
}

void TransmissionPage::setFileName(std::string filename)
{
    ui->fileName->setText(QString::fromStdString(filename));
}

void TransmissionPage::on_pauseButton_clicked()
{
    if(m_contral){
        m_contral->paused = true;
    }
}

void TransmissionPage::setProgressBar(int id,int prog)
{
    if(id == windowId){
        ui->progressBar->setValue(prog);
    }
    if(100 == prog){
        qDebug()<<"load over";
        emit funishSignal(windowId,ui->fileName->text().toStdString());
        close();
    }
}

void TransmissionPage::on_continueButton_clicked()
{
    if(m_contral){
        {
            std::lock_guard<std::mutex> lk(m_contral->mtx);
            m_contral->paused = false;
        }
        m_contral->cv.notify_one();
    }
}

void TransmissionPage::on_cancelButton_clicked()
{
    if(m_contral){
        {
            std::lock_guard<std::mutex> lk(m_contral->mtx);
            m_contral->cancelled = true;
        }
        m_contral->cv.notify_one();
    }
}
